<template>
  <div class="distributionCoupon">
    <c-title :hide="false" text="配销券"> </c-title>
    <van-tabs v-model="active" @change="tabChange">
      <van-tab title="待使用" name="0"></van-tab>
      <van-tab title="已使用" name="1"></van-tab>
      <van-tab title="已过期" name="-1"></van-tab>
    </van-tabs>
    <div class="couponList">
      <template v-if="active == '0'">
        <div class="lis" v-for="(item, index) in listData" :key="index">
          <div class="lis_l">
            <div class="circle_top"></div>
            <div class="circle_bottom"></div>
            <div class="money" v-if="item.coupon">
              <span class="small">￥</span>
              <span style="line-height: 1.5rem;">{{ item.coupon.price }}</span>
            </div>
          </div>
          <div class="lis_r">
            <div class="detail" v-if="item.coupon">
              <div class="title">{{ item.coupon.name }}</div>
              <div class="time">{{ item.coupon.active_start }}至{{ item.coupon.active_end }}</div>
            </div>
            <div class="btn_open" @click="toSearch(item.coupon_id)">去使用</div>
          </div>
        </div>
      </template>
      <template v-else>
        <div class="lis" v-for="(item, index) in listData" :key="index" style="color:#6E6E79;">
          <div class="lis_l">
            <div class="circle_top"></div>
            <div class="circle_bottom"></div>
            <div class="money" style="color:#6E6E79;" v-if="item.coupon">
              <span class="small">￥</span>
              <span style="line-height: 1.5rem;">{{ item.coupon.price }}</span>
            </div>
          </div>
          <div class="lis_r">
            <div class="detail" v-if="item.coupon">
              <div class="title">{{ item.coupon.name }}</div>
              <div class="time" v-if="active == '1'">{{ item.coupon.active_start }}</div>
              <div class="time" v-if="active == '-1'">{{ item.coupon.active_start }}至{{ item.coupon.active_end }}</div>
            </div>
          </div>
        </div>
      </template>
      <van-empty image="search" description="暂无更多" v-if="listData.length <= 0" />
    </div>
  </div>
</template>
<script>
import { scrollMixin } from "utils/mixin";
import { Toast } from "vant";
export default {
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      active: "0",
      listData: [],
      page: 1,
      isLoadMore: true,
      total_page: 0
    };
  },
  activated() {
    this.init();
    this.getData();
  },
  methods: {
    init(){
      this.page = 1;
      this.total_page = 0;
      this.isLoadMore = true;
      this.listData = [];
    },
    toSearch(coupon_id) {
      this.$router.push(this.fun.getUrl("distributionCouponSearch", { coupon_id: coupon_id }, {}));
    },
    tabChange() {
      this.page = 1;
      this.total_page = 0;
      this.isLoadMore = true;
      this.listData = [];
      this.getData();
    },
    getData() {
      $http.get("plugin.distribution-coupon.api.coupon.index", { page: this.page, status: this.active }, "加载中").then(response => {
        if (response.result == 1) {
          this.listData = response.data.data;
          this.isLoadMore = true;
          this.total_page = response.data.last_page;
          if (!this.total_page) {
            this.total_page = 0;
          }
        } else {
          Toast(response.msg);
        }
      });
    },
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      let json = {
        page: this.page
      };
      if (this.page >= this.total_page) {
        this.isLoadMore = false;
        return;
      } else {
        this.isLoading = true;
        this.page = this.page + 1;
        json.page = this.page;
        $http
          .get("plugin.distribution-coupon.api.coupon.index", json, "加载中")
          .then(response => {
            this.isLoadMore = true;
            if (response.result === 1) {
              this.listData = this.listData.concat(response.data.data);
            } else {
              this.page = this.page - 1;
              this.isLoadMore = false;
              return;
            }
          })
          .catch(err => {
            console.log(err);
          });
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.distributionCoupon{
  padding-bottom: 3rem;
}
.couponList {
  .lis {
    margin: 0.63rem 0.75rem;
    background: #fff;
    display: flex;
    justify-content: space-between;
    border-radius: 0.625rem;
    .lis_r {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 0.63rem 0 0.75rem;
      .detail {
        flex: 1;
        display: flex;
        flex-direction: column;
        .title {
          font-size: 0.88rem;
          font-weight: bold;
          text-align: left;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
        }
        .time {
          font-size: 0.75rem;
          text-align: left;
          line-height: 1.25rem;
          margin-top: 0.94rem;
          color: #6e6e79;
        }
      }
      .btn_open {
        flex-shrink: 0;
        font-size: 0.75rem;
        color: #ffffff;
        background: #f15353;
        border-radius: 0.88rem;
        height: 1.75rem;
        width: 4.06rem;
        line-height: 1.75rem;
        margin-left: 0.5rem;
      }
    }
    .lis_l {
      height: 5.63rem;
      width: 6.81rem;
      border-radius: 0.625rem;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 1.5rem;
      font-weight: bold;
      color: #f15353;
      position: relative;
      .money {
        display: flex;
        align-items: flex-end;
        .small {
          font-size: 0.6rem;
        }
      }
      .circle_top {
        position: absolute;
        width: 0.5625rem;
        height: 0.5625rem;
        border-radius: 50%;
        left: 6.528rem;
        top: -0.2813rem;
        background: #f5f5f5;
      }
      .circle_bottom {
        position: absolute;
        width: 0.5625rem;
        height: 0.5625rem;
        border-radius: 50%;
        left: 6.528rem;
        bottom: -0.2813rem;
        background: #f5f5f5;
      }
    }

    .lis_l::before {
      content: "";
      position: absolute;
      right: 0;
      top: 0.5rem;
      width: 1px;
      height: 4.63rem;
      border-left: 1px dashed #d6d6dc;
    }
  }
}
</style>
